package com.exam.sacrifice.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

public class AdvancedGlideUtil {

    private static RequestOptions getDefaultOptions() {
        return new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .skipMemoryCache(false);
    }

    /**
     * 加载图片（完整参数）
     */
    public static void loadImage(Context context, Object source, ImageView imageView,
                                 @DrawableRes int placeholder, @DrawableRes int error,
                                 boolean skipMemoryCache, int overrideWidth, int overrideHeight,
                                 RequestListener<Drawable> listener) {
        RequestOptions options = getDefaultOptions()
                .placeholder(placeholder)
                .error(error)
                .skipMemoryCache(skipMemoryCache);

        if (overrideWidth > 0 && overrideHeight > 0) {
            options = options.override(overrideWidth, overrideHeight);
        }

        RequestBuilder<Drawable> builder = Glide.with(context)
                .load(source)
                .apply(options);

        if (listener != null) {
            builder = builder.listener(listener);
        }

        builder.into(imageView);
    }

    /**
     * 加载圆形图片
     */
    public static void loadCircleImage(Context context, Object source, ImageView imageView,
                                       @DrawableRes int placeholder, @DrawableRes int error) {
        Glide.with(context)
                .load(source)
                .apply(getDefaultOptions()
                        .placeholder(placeholder)
                        .error(error)
                        .transform(new CircleCrop()))
                .into(imageView);
    }

    /**
     * 加载圆角图片
     */
    public static void loadRoundImage(Context context, Object source, ImageView imageView,
                                      @DrawableRes int placeholder, @DrawableRes int error,
                                      int radius) {
        Glide.with(context)
                .load(source)
                .apply(getDefaultOptions()
                        .placeholder(placeholder)
                        .error(error)
                        .transform(new RoundedCorners(radius)))
                .into(imageView);
    }

    /**
     * 加载GIF
     */
    public static void loadGif(Context context, Object source, ImageView imageView) {
        Glide.with(context)
                .asGif()
                .load(source)
                .apply(getDefaultOptions())
                .into(imageView);
    }

    /**
     * 获取Bitmap
     */
    public static void loadAsBitmap(Context context, Object source, final BitmapCallback callback) {
        Glide.with(context)
                .asBitmap()
                .load(source)
                .apply(getDefaultOptions())
                .listener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model,
                                                Target<Bitmap> target, boolean isFirstResource) {
                        if (callback != null) {
                            callback.onLoadFailed(e);
                        }
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, Object model,
                                                   Target<Bitmap> target, DataSource dataSource,
                                                   boolean isFirstResource) {
                        if (callback != null) {
                            callback.onResourceReady(resource);
                        }
                        return false;
                    }
                })
                .submit();
    }

    public interface BitmapCallback {
        void onResourceReady(Bitmap bitmap);
        void onLoadFailed(Exception e);
    }
}